<template>
  <div class="home">
    <div class="father">
      <el-container class="home-container">
        <el-header>
          <div>
            <img src="../assets/heima.png" alt="" />
            <span>欢迎来到首页</span>
          </div>
          <el-button type="info" @click="loginout">退出</el-button>
        </el-header>
        <el-container>
          <el-aside :width="isCollapse ? '64px' : '200px'">
            <div class="toggle-button" @click="toggleCollapse">|||</div>
            <!-- 侧边栏菜单 -->
            <el-menu
              background-color="#333744"
              text-color="#fff"
              active-text-color="#409EFF"
              :unique-opened="true"
              :collapse="isCollapse"
              :collapse-transition="false"
              :router="true"
              :default-active="activePath"
            >
              <!-- 一级菜单 -->
              <el-submenu
                :index="item.id + ''"
                v-for="item in menuList"
                :key="item.id"
              >
                <!-- 一级菜单模板 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i :class="iconsObj[item.id]"></i>
                  <!-- 文本 -->
                  <span>{{ item.authName }}</span>
                </template>
                <!-- 二级子菜单 -->
                <el-menu-item
                  :index="'/' + subitem.path"
                  v-for="subitem in item.children"
                  :key="subitem.id"
                  @click="saveNavState('/' + subitem.path)"
                >
                  <!-- 二级菜单模板 -->
                  <template slot="title">
                    <!-- 图标 -->
                    <i class="el-icon-menu"></i>
                    <!-- 文本 -->
                    <span>{{ subitem.authName }}</span>
                  </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </el-aside>
          <el-main>
            <router-view></router-view>
          </el-main>
        </el-container>
      </el-container>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao',
      },
      // 是否折叠
      isCollapse: false,
      // 子菜单是否激活
      activePath: '',
    };
  },
  name: 'Home',
  // 左侧菜单栏的数据在页面一加载就需要请求回来，因此这个请求的方法需要定义在生命周期的created函数里面，并且这个不需要操作dom,因此不需要等待着vue解析完成之后（mounted）阶段来定义这个方法
  created() {
    this.getMenuList();
    // 取出保存的菜单栏地址
    // 在整个Home组件一被创建的时候就立即从sessionStorage取出这个值
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    loginout() {
      // 清除token，并且跳转至登录页面
      window.sessionStorage.clear();
      this.$router.push('/login');
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus');
      // 如果请求发送失败则提示一下失败信息
      if (res.meta.status !== 200) {
        return this.$Elementui.Message.error(res.meta.msg);
      }
      // console.log(res);
      this.menuList = res.data;
    },
    // 点击切换侧边栏的显示和隐藏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath);
      // 点过其他二级菜单之后，再回来点用户列表菜单，无法激活，因此在点击事件函数里面重新赋值
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 2000px;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
